<template>
  <div>
    <div>
      <FormPanel
        ref="moreInfo"
        :list="formList"
        :cols="3"
        :formType="type"
        labelWidth="110"
        :isSubmitBtn="false"
        @formChange="formChangeHandle"
        style="margin-top: 10px"
      />
    </div>
    <!-- 转介绍客户弹框 -->
    <BaseDialog
      :visible.sync="visibleNAME"
      title="转介绍客户选择(精确查询)"
      destroyOnClose
      :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}"
    >
      <searchName :type="handleType" :rowData="formDataNAME" @close="closeHandler" @NAMEHandle="NAMEHandle"/>
    </BaseDialog>

  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import searchName from './searchName';
import { getlistAllData} from "@/api/common/org/dealerCompany";
import { IDNumber, phone, email, formatDates, formatDat } from '@/utils/validate'
import moment from 'moment'
export default {
  name: 'moreInfo',
  mixins: [dictionary],
  components: {
    searchName
  },
  props: {
    type: {
      type: String,
      default: 'show'
    },
    rowData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      formList: this.createFormList(),
      saveData: null,
      listAllData:[],
      provinceId: '',
      cityId: '',
      visibleNAME: false,
      formDataNAME: {},
      handleType: '',
      introducCustomerId: null, // 转介绍客户id
    }
  },
  created () {
    console.log('更多信息created',this.rowData, this.type)
    this.init()
    this.fillAddress()
  },
  methods: {
    init () {
      console.log('init',this.rowData, this.type)
      console.log('更多信息的introducCustomerId',this.introducCustomerId)
      let { rowData } = this
      this.provinceId = rowData.province
      this.cityId = rowData.city
      this.formList.forEach(item => {
        if (item.fieldName === 'birthday') {
          if(rowData['birthday']){
            item.initialValue = formatDat(rowData['birthday'])
          }
        } else if (item.fieldName === 'haveChildren'){
          if(rowData.haveChildren){
            item.initialValue = Number(rowData.haveChildren)
          }
        } else if (item.fieldName === 'postName'){
          if(rowData.postName){
            item.initialValue = Number(rowData.postName)
          }
        } else if (item.fieldName === 'avocation'){
          if(rowData.avocation){
            item.initialValue = Number(rowData.avocation)
          }
        } else if (item.fieldName === 'avocation'){
          if(rowData.avocation){
            item.initialValue = Number(rowData.avocation)
          }
        } else if (item.fieldName === 'introducCustomerName'){
          item.initialValue = `${rowData['introducCustomerName'] || ''}${rowData['introducCustomerPhone'] || ''}`
          // this.introducCustomerId = rowData['introducCustomerId']
        }
        else {
          item.initialValue = rowData[item.fieldName]
        }
      })
    },
    // 省市县区
    fillAddress () {
      getlistAllData().then(res=>{
        console.log('城市', res)
        let province=[]
        let initCity=[]
        let initCounty=[]
        this.listAllData=res.data
        this.listAllData.forEach(x=>{
          // 筛选省
          if(x.parentRegionId == null){
            province.push({text:x.regionName,value:`${x.regionId}`})
          }
          // 市
          if(x.parentRegionId == this.provinceId){
            initCity.push({text:x.regionName,value:`${x.regionId}`})
          }
          if(x.parentRegionId == this.cityId){
            initCounty.push({text:x.regionName,value:`${x.regionId}`})
          }
        })
        this.formList.find(item => item.fieldName === 'province').itemList=province
        this.formList.find(item => item.fieldName === 'city').itemList=initCity
        this.formList.find(item => item.fieldName === 'area').itemList=initCounty
        // this.listAllData.forEach(x=>{
        //   let proviceValue = this.formList.find(x => x.fieldName === 'province').initialValue
        //   let cityValue = this.formList.find(x => x.fieldName === 'city').initialValue
        //   // 筛选省
        //   if(x.parentRegionId == null){
        //     this.province.push({text:x.regionName,value:x.regionId})
        //   }
        //   // 筛选市
        //   if(proviceValue && x.parentRegionId == proviceValue){
        //     this.city.push({text:x.regionName,value:x.regionId})
        //   }
        //   // 筛选区
        //   if(cityValue && x.parentRegionId == cityValue ){
        //     this.district.push({text:x.regionName,value:x.regionId})
        //   }
        // })
        // this.formList.find(item => item.fieldName === 'province').itemList = _.uniqBy(this.province,'value')
        // this.formList.find(item => item.fieldName === 'city').itemList = _.uniqBy(this.city,'value')
        // this.formList.find(item => item.fieldName === 'area').itemList = _.uniqBy(this.district,'value')
      })
    },
    createFormList () {
      return [
        {
          type: 'SELECT',
          label: '证件类型',
          fieldName: 'ctCode',
          placeholder: '请选择',
          itemList: this.createDictList('1508'),
          initialValue: ''
        },
        {
          type: 'INPUT',
          label: '证件号码',
          fieldName: 'certificateNo',
          placeholder: '请输入...',
          initialValue: ''
        },
        {
          type: 'DATE',
          label: '出生日期',
          fieldName: 'birthday',
          dateType: 'exactdate',
          placeholder: '',
        },
        {
          type: 'SELECT',
          label: '省份',
          fieldName: 'province', // provinceName
          placeholder: '请选择',
          initialValue: '',
          itemList: [],
          change: val => {
            let city=[]
            if(val == 3662){
              this.formList.find(x => x.fieldName === 'city').itemList=[{text:"台湾",value:3662}]
              this.formList.find(x => x.fieldName === 'area').itemList=[{text:"台湾",value:3662}]
            }else if(val == 3663){
              this.formList.find(x => x.fieldName === 'city').itemList=[{text:"香港",value:3663}]
              this.formList.find(x => x.fieldName === 'area').itemList=[{text:"香港",value:3663}]
            }else if(val == 3664){
              this.formList.find(x => x.fieldName === 'city').itemList=[{text:"澳门",value:3664}]
              this.formList.find(x => x.fieldName === 'area').itemList=[{text:"澳门",value:3664}]
            }
            this.formList.find(x => x.fieldName === 'city').initialValue=''
            this.formList.find(x => x.fieldName === 'city').itemList= []
            this.formList.find(x => x.fieldName === 'area').initialValue=''
            this.formList.find(x => x.fieldName === 'area').itemList= []
            this.listAllData.forEach(x=>{
              // 筛选省
              if(x.parentRegionId == val){
                city.push({text:x.regionName,value:x.regionId})
                this.formList.find(x => x.fieldName === 'city').itemList=_.uniqBy(city,'value')
              }
            })
          },
        },
        {
          type: 'SELECT',
          label: '城市',
          fieldName: 'city', // cityName
          placeholder: '请选择',
          initialValue: '',
          itemList: [],
          change: val =>{
            let county=[]
            this.formList.find(x => x.fieldName === 'area').initialValue=''
            this.formList.find(x => x.fieldName === 'area').itemList= []
            this.listAllData.forEach(x=>{
              // 筛选省
              if(x.parentRegionId == val){
                county.push({text:x.regionName,value:x.regionId})
                this.formList.find(x => x.fieldName === 'area').itemList=_.uniqBy(county,'value')
              }
            })
          }
        },
        {
          type: 'SELECT',
          label: '区域',
          fieldName: 'area', // areaName
          placeholder: '请选择',
          itemList: [],
          initialValue: ''
        },
        {
          type: 'INPUT',
          label: '详细地址',
          fieldName: 'address',
          selfCols: 2,
          placeholder: '请输入...',
          initialValue: ''
        },
        {
          type: 'INPUT',
          label: '电子邮箱',
          fieldName: 'eMail',
          placeholder: '请输入...',
          initialValue: ''
        },
        {
          type: 'INPUT',
          label: 'QQ',
          fieldName: 'qq',
          placeholder: '请输入...',
          initialValue: ''
        },
        {
          type: 'INPUT',
          label: '微信',
          fieldName: 'wechat',
          placeholder: '请输入...',
          initialValue: ''
        },
        {
          type: 'INPUT',
          label: '固定电话',
          fieldName: 'phone',
          placeholder: '请输入...',
          initialValue: ''
        },
        {
          type: 'SELECT',
          label: '喜欢的联系方式',
          fieldName: 'likeContactWay',
          placeholder: '请选择',
          itemList: this.createDictList('7058'),
          initialValue: ''
        },
        {
          type: 'SELECT',
          label: '教育水平',
          fieldName: 'educationalLevel',
          placeholder: '请选择',
          itemList: this.createDictList('7027'),
          initialValue: ''
        },
        {
          type: 'SELECT',
          label: '婚姻状况',
          fieldName: 'maritalStatus',
          placeholder: '请选择',
          itemList: this.createDictList('7028'),
        },
        {
          type: 'SELECT',
          label: '日常驾乘人数',
          fieldName: 'drivingNumber',
          placeholder: '请选择',
          itemList: this.createDictList('7059'),
        },
        {
          type: 'SELECT',
          label: '是否有子女',
          fieldName: 'haveChildren',
          placeholder: '请选择',
          itemList: this.createDictList('1004'),
        },
        {
          type: 'SELECT',
          label: '子女数量',
          fieldName: 'childrenNumber',
          placeholder: '请选择',
          itemList: this.createDictList('7060'),
        },
        {
          type: 'INPUT_NUMBER',
          label: '家庭人数',
          fieldName: 'familyNum',
          placeholder: '请输入...'
        },
        {
          type: 'INPUT_NUMBER',
          label: '家庭年收入(万)',
          fieldName: 'familyInCome',
          placeholder: '请输入...',
        },
        {
          type: 'SELECT',
          label: '居住地类型',
          fieldName: 'liveType',
          placeholder: '请选择',
          itemList: this.createDictList('7061'),
          initialValue: ''
        },
        {
          type: 'SELECT',
          label: '工作地类型',
          fieldName: 'workPlaceType',
          placeholder: '请选择',
          itemList: this.createDictList('7062'),
          initialValue: ''
        },
        {
          type: 'SELECT',
          label: '企业性质',
          fieldName: 'enterpriseType',
          placeholder: '请选择',
          itemList: this.createDictList('7029'),
          initialValue: ''
        },
        {
          type: 'SELECT',
          label: '所属行业',
          fieldName: 'industry',
          placeholder: '请选择',
          itemList: this.createDictList('7030'),
          initialValue: ''
        },
        {
          type: 'SELECT',
          label: '职业',
          fieldName: 'postName',
          placeholder: '请选择',
          itemList: this.createDictList('7031'),
          initialValue: ''
        },
        {
          type: 'SELECT',
          label: '是否吸烟',
          fieldName: 'smokingOrNot',
          placeholder: '请选择',
          itemList: this.createDictList('1004'),
          initialValue: ''
        },
        {
          type: 'SELECT',
          label: '个人爱好',
          fieldName: 'avocation',
          placeholder: '请选择',
          itemList: this.createDictList('7032'),
          initialValue: ''
        },
        {
          type: 'SELECT',
          label: '钟爱饮料',
          fieldName: 'favoriteDrinks',
          placeholder: '请选择',
          itemList: this.createDictList('7033'),
          initialValue: ''
        },
        {
          type: 'SELECT',
          label: '休眠客户',
          fieldName: 'isDormant',
          placeholder: '请选择',
          itemList: [{value: 1,text:'是'}, {value: 0,text:'否'}],
          initialValue: ''
        },
        {
          type: 'INPUT',
          label: '转介绍客户',
          fieldName: 'introducCustomerName',
          placeholder: '',
          initialValue: '',
          readonly: true,
          unitRender: () => {
            return <el-button slot="append" icon="el-icon-search" onClick={this.openReferral}></el-button>;
          },
        },

      ]
    },
    // 转介绍
    openReferral(){
      this.visibleNAME = true;
    },
    // 转介绍
    NAMEHandle(data){
      console.log("转介绍",data)
      if(data){
        let aimString = `${data.customerName} ${data.mobilePhone}`
        // 需要提交的转介绍客户id
        this.introducCustomerId = data.potentialCustomersId
        console.log("转介绍客户的id", this.introducCustomerId)
        this.formList.find(item => item.fieldName == 'introducCustomerName').initialValue = aimString
        this.$emit('myClallback',this.introducCustomerId)
      }
    },
    closeHandler(val, type) {
      this.visibleNAME = val;
      if (type === 'refresh') {
        // 重新加载
        this.fetchParams = { ...this.fetchParams };
      }
    },
    formChangeHandle(val) {
      this.saveData = val;
    },
  }
}
</script>

<style lang="less" scoped>

</style>
